import Vue from 'vue';
import Router from 'vue-router';
import Login from '@/components/Login/Login';
import Default from '@/components/Layout/Default';
import welcome from '@/components/Layout/Content/welcome';
// need模块
import management from '@/components/Layout/Content/need/management';
import count from '@/components/Layout/Content/need/count';
import myself from '@/components/Layout/Content/need/myself';
// ucloud2.0模块
import area from '@/components/Layout/Content/ucloud2.0/area';
import node from '@/components/Layout/Content/ucloud2.0/node';
import config from '@/components/Layout/Content/ucloud2.0/config';
import publish from '@/components/Layout/Content/ucloud2.0/publish';
import apply from '@/components/Layout/Content/ucloud2.0/apply';

Vue.use(Router)

let router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/matrix',
            name: 'matrix',
            title: '首页',
            component: Default,
            children: [
                {
                    path: '/matrix/',
                    name: 'welcome',
                    title: '欢迎页',
                    meta: {
                        requireAuth: true,
                    },
                    component: welcome
                },
                // need模块
                {
                    path: '/matrix/need/management',
                    name: 'management',
                    prevAlias: 'need',
                    prevTitle: '需求管理',
                    title: '需求管理',
                    meta: {
                        requireAuth: true,
                    },
                    component: management
                },
                {
                    path: '/matrix/need/count',
                    name: 'count',
                    prevAlias: 'need',
                    prevTitle: '需求管理',
                    title: '需求统计',
                    meta: {
                        requireAuth: true,
                    },
                    component: count
                },
                {
                    path: '/matrix/need/myself',
                    name: 'myself',
                    prevAlias: 'need',
                    prevTitle: '需求管理',
                    title: '我的需求',
                    meta: {
                        requireAuth: true,
                    },
                    component: myself
                },
                // ucloud2.0模块
                {
                    path: '/matrix/ucloud2.0/area',
                    name: 'area',
                    prevAlias: 'ucloud2',
                    prevTitle: 'Ucloud2.0',
                    title: '区域管理',
                    meta: {
                        requireAuth: true,
                    },
                    component: area
                },
                {
                    path: '/matrix/ucloud2.0/node',
                    name: 'node',
                    prevAlias: 'ucloud2',
                    prevTitle: 'Ucloud2.0',
                    title: '节点管理',
                    meta: {
                        requireAuth: true,
                    },
                    component: node
                },
                {
                    path: '/matrix/ucloud2.0/config',
                    name: 'config',
                    prevAlias: 'ucloud2',
                    prevTitle: 'Ucloud2.0',
                    title: '应用配置',
                    meta: {
                        requireAuth: true,
                    },
                    component: config
                },
                {
                    path: '/matrix/ucloud2.0/publish',
                    name: 'publish',
                    prevAlias: 'ucloud2',
                    prevTitle: 'Ucloud2.0',
                    title: '发布面板',
                    meta: {
                        requireAuth: true,
                    },
                    component: publish
                },
                {
                    path: '/matrix/ucloud2.0/apply',
                    name: 'apply',
                    prevAlias: 'ucloud2',
                    prevTitle: 'Ucloud2.0',
                    title: '申请列表',
                    meta: {
                        requireAuth: true,
                    },
                    component: apply
                },
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
            if(sessionStorage.getItem("tokenId")) {
                next();
            }else {
                next({
                    path: '/login',
                    query: {redirect: to.fullPath}    // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
    }
    else {
        next();
    }
})

export default router;